package com.hsogoo.avatar.demo.handler;

import com.google.gson.Gson;
import com.hsogoo.avatar.model.AvatarResponse;
import com.hsogoo.avatar.transport.AvatarFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;

/**
 * @Author huangsaigang
 * Created @2018/10/1.
 */
public class DemoClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("on connect...");

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("client got message :" + msg);
        AvatarResponse response = new Gson().fromJson(msg.toString(), AvatarResponse.class);
        AvatarFuture.setResponse(response);
        if("ping".equals(msg)){
            ctx.writeAndFlush("pong\r\n");
            return;
        }
        ctx.close();
    }
}
